package com.example.kehoobackend.controller;


import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.domain.Topic;
import com.example.kehoobackend.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@CrossOrigin
@RestController
public class TopicController {
    @Autowired
    private TopicService topicService;

    /**
     * 查询所有一级话题
     *
     * @return CommonResponse<List < Topic>>
     */
    @GetMapping("/topic")
    public CommonResponse<List<Topic>> getAllTopic() {
        return topicService.getAllTopTopic();
    }

    /**
     * 查询父话题
     *
     * @param topic_id 话题id
     * @return CommonResponse<List < Topic>>
     */
    @GetMapping("/topic/parent_topic_id")
    public CommonResponse<Topic> getParentTopicByTopicId(
            @RequestParam("topic_id") Integer topic_id) {
        return topicService.getParentTopicByTopicId(topic_id);
    }

    /**
     * 查询子话题
     *
     * @param topic_id 话题id
     * @return CommonResponse<List < Topic>>
     */
    @GetMapping("/topic/topic_id")
    public CommonResponse<List<Topic>> getChildTopicByTopicId(
            @RequestParam("topic_id") Integer topic_id) {
        return topicService.getChildTopicByTopicId(topic_id);
    }

    /**
     * 查询所有话题
     *
     * @return CommonResponse<List < Topic>>
     */
    @GetMapping("/topic/all")
    public CommonResponse<List<Topic>> getAllTopicList() {
        return topicService.getAllTopicList();
    }




    /**
     * 新增话题
     *
     * @param topic 话题
     * @return CommonResponse<Boolean>
     */
    @PostMapping("/topic")
    public CommonResponse<Topic> addTopic(
            @RequestBody Topic topic) {
        return CommonResponse.createForSuccess("新增话题成功", topicService.addTopic(topic));
    }




    /**
     * 更改话题图片
     *
     * @param topic_image 话题
     * @return CommonResponse<Boolean>
     */
    @PatchMapping("/topic/topic_image")
    public CommonResponse<Boolean> updateTopicImage(
            @RequestParam("topic_id") Integer topic_id,
            @RequestParam("topic_image") String topic_image) {
        return topicService.updateTopicImage(topic_id, topic_image)
                ? CommonResponse.createForSuccess("更改话题图片成功", true)
                : CommonResponse.createForSuccess("更改话题图片失败", false);
    }

    /**
     * 更改话题描述
     *
     * @param topic_desc 话题描述
     * @return CommonResponse<Boolean>
     */
    @PatchMapping("/topic/topic_desc")
    public CommonResponse<Boolean> updateTopicDescription(
            @RequestParam("topic_id") Integer topic_id,
            @RequestParam("topic_desc") String topic_desc) {
        return topicService.updateTopicDescription(topic_id, topic_desc)
                ? CommonResponse.createForSuccess("更改话题描述成功", true)
                : CommonResponse.createForSuccess("更改话题描述失败", false);
    }

    /**
     * 更改话题名称
     *
     * @param topic_name 话题
     * @return CommonResponse<Boolean>
     */
    @PatchMapping("/topic/topic_name")
    public CommonResponse<Boolean> updateTopicName(
            @RequestParam("topic_id") Integer topic_id,
            @RequestParam("topic_name") String topic_name) {
        return topicService.updateTopicName(topic_id, topic_name)
                ? CommonResponse.createForSuccess("更改话题名称成功", true)
                : CommonResponse.createForSuccess("更改话题名称失败", false);
    }


}
